package com.bawei.module_main.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentController
import androidx.fragment.app.FragmentPagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.library_base.AppManager
import com.bawei.library_common.arouter.ARouterActivityPath
import com.bawei.library_common.arouter.ARouterFragmentPath
import com.bawei.library_common.mvvm.BaseActivity
import com.bawei.module_main.R
import com.bawei.module_main.adapter.MainPagerAdapter
import com.bawei.module_main.databinding.MainActivityMainBinding
import com.blankj.utilcode.util.ToastUtils
import me.majiajie.pagerbottomtabstrip.NavigationController

@Route(path = ARouterActivityPath.PAGER_MAIN)
class MainActivity : BaseActivity<MainActivityMainBinding>() {
    private lateinit var mainPagerAdapter: MainPagerAdapter
    private lateinit var list:ArrayList<Fragment>
    private lateinit var controller: NavigationController
    override fun getLayout(): Int {
        return R.layout.main_activity_main
    }

    override fun initData() {
        var height = geStatusBartHeight()
        ToastUtils.showShort("$height")
        var layoutParams = viewDataBinding.viewPager.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.topMargin = height
        viewDataBinding.viewPager.layoutParams = layoutParams

        initFragments()
        initBottomPagerView()

        //将闪屏页和广告页结束
        AppManager.finishActivity(SplashActivity::class.java)
        AppManager.finishActivity(GuideActivity::class.java)

    }

    private fun initBottomPagerView() {
        //底部导航
        controller = viewDataBinding.bottom.material().apply {
            addItem(R.drawable.main_home,"首页",ContextCompat.getColor(this@MainActivity,R.color.main_bottom_check_color))
            addItem(R.drawable.main_community,"社区",ContextCompat.getColor(this@MainActivity,R.color.main_bottom_check_color))
            addItem(R.drawable.main_notify,"通知",ContextCompat.getColor(this@MainActivity,R.color.main_bottom_check_color))
            addItem(R.drawable.main_user,"用户",ContextCompat.getColor(this@MainActivity,R.color.main_bottom_check_color))
            setDefaultColor(ContextCompat.getColor(this@MainActivity,R.color.main_bottom_default_color))//默认颜色
            enableAnimateLayoutChanges()//动画
        }.build()
        controller.setHasMessage(1,true)//红点
        controller.setMessageNumber(2,123)//未读消息个数
        controller.setupWithViewPager(viewDataBinding.viewPager)//上下联动

    }

    private fun initFragments() {
        list = ArrayList<Fragment>()
        var homeFragment =  ARouter.getInstance().build(ARouterFragmentPath.PAGER_HOME).navigation()
        var communityFragment =  ARouter.getInstance().build(ARouterFragmentPath.PAGER_COMMUNITY).navigation()
        var moreFragment =  ARouter.getInstance().build(ARouterFragmentPath.PAGER_MORE).navigation()
        var userFragment =  ARouter.getInstance().build(ARouterFragmentPath.PAGER_USER).navigation()
        list.add(homeFragment as Fragment)
        list.add(communityFragment as Fragment)
        list.add(moreFragment as Fragment)
        list.add(userFragment as Fragment)
        mainPagerAdapter = MainPagerAdapter(list,supportFragmentManager,FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
        viewDataBinding.viewPager.adapter = mainPagerAdapter
    }

    override fun initListener() {

    }

}